package com.ymatou.iapi.liveseller.testcase;

import java.util.Date;
import java.util.LinkedHashMap;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.liveseller.parameter.RemoveProductVideoBean;
import com.ymatou.iapi.liveseller.service.RemoveProductVideoCall;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CounterCallService;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.PayGateWayCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.livetv.ProductVideo;
import com.ymttest.database.model.livetv.ProductVideoExplain;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*取消录制任务Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_RemoveProductVideo{
	private static RemoveProductVideoBean removeproductvideoBean;
	private static RemoveProductVideoCall removeproductvideoCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static LiveRoomWapper liveRoomWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("取消录制任务Test");
	}

	@Before
	public void caseUp() {
		removeproductvideoBean=new RemoveProductVideoBean();
		removeproductvideoCall=new RemoveProductVideoCall();
		liveRoomWapper=new LiveRoomWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(removeproductvideoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RemoveProductVideo_001() {
		Logger.start(true,"开始录制，无待讲解时候 取消录制任务，状态是录制失败");
		try {
			
			//创建直播，
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 1秒钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			Date starttime = new Date(now.getTime() + 3000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(livedto.getId(), sellerId, productId);
			Thread.sleep(5000);
			LiveRoomCallService.SellerLiveOnline(livedto.getId(), sellerId);
			//开始直播录制
			String taskId = LiveRoomCallService.CreateProductVideo(livedto.getId(), sellerId, productId);
			//增加mock，消除存量数据
			LinkedHashMap<String,Object> m=new LinkedHashMap<>();
			m.put("mock", "mock-enable");
			m.put("mockId-1", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-1", "liveseller-stopLiveRecord-Normal");
			PayGateWayCallService.addMock(removeproductvideoCall, m);
			
			//结束录制
			removeproductvideoBean.setProductId(productId);
			removeproductvideoBean.setLiveId(livedto.getId().intValue());
			
			removeproductvideoCall.setData(removeproductvideoBean);
			removeproductvideoCall.callService();			
			Logger.verifyEquals("200", removeproductvideoCall.getString("Code"), "验证code");
			ProductVideoExplain pv = liveRoomWapper.selectProductvideoExplain(productId);
			Logger.verifyEquals(pv.getUserId(),sellerId , "验证UserId");
			Logger.verifyEquals(pv.getTaskId()+"",taskId , "验证TaskId");
			Logger.verifyEquals(pv.getLiveId(),livedto.getId() , "验证LiveId");
			Logger.verifyEquals(pv.getExplainStatus(),3 , "验证ExplainStatus");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RemoveProductVideo_002() {
		Logger.start(true,"开始录制，先终止录制，然后取消录制任务，成功");
		try {
			//创建直播，
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 1秒钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			Date starttime = new Date(now.getTime() + 3000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(livedto.getId(), sellerId, productId);
			Thread.sleep(5000);
			LiveRoomCallService.SellerLiveOnline(livedto.getId(), sellerId);
			//开始直播录制
			String taskId = LiveRoomCallService.CreateProductVideo(livedto.getId(), sellerId, productId);
			LiveRoomCallService.StopProductVideo(livedto.getId(), productId);
			//增加mock，消除存量数据
			LinkedHashMap<String,Object> m=new LinkedHashMap<>();
			m.put("mock", "mock-enable");
			m.put("mockId-1", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-1", "liveseller-stopLiveRecord-Normal");
			PayGateWayCallService.addMock(removeproductvideoCall, m);
			
			//结束录制
			removeproductvideoBean.setProductId(productId);
			removeproductvideoBean.setLiveId(livedto.getId().intValue());
			
			removeproductvideoCall.setData(removeproductvideoBean);
			removeproductvideoCall.callService();			
			Logger.verifyEquals("200", removeproductvideoCall.getString("Code"), "验证code");
			ProductVideoExplain pv = liveRoomWapper.selectProductvideoExplain(productId);
			Logger.verifyEquals(pv.getUserId(),sellerId , "验证UserId");
			Logger.verifyEquals(pv.getTaskId()+"",taskId , "验证TaskId");
			Logger.verifyEquals(pv.getLiveId(),livedto.getId() , "验证LiveId");
			Logger.verifyEquals(pv.getExplainStatus(),4, "验证ExplainStatus");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RemoveProductVideo_003() {
		Logger.start(true,"开始录制，在有待讲解时候 取消录制任务，状态是待讲解");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(sellerId));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			//求讲解
			LiveRoomCallService.AskProductVideo(createlive.getId(), pid, 1);
			//开始直播录制
			String taskId = LiveRoomCallService.CreateProductVideo(createlive.getId(), sellerId, pid);
			//增加mock，消除存量数据
			LinkedHashMap<String,Object> m=new LinkedHashMap<>();
			m.put("mock", "mock-enable");
			m.put("mockId-1", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-1", "liveseller-stopLiveRecord-Normal");
			PayGateWayCallService.addMock(removeproductvideoCall, m);
			
			//结束录制
			removeproductvideoBean.setProductId(pid);
			removeproductvideoBean.setLiveId(createlive.getId().intValue());
			
			removeproductvideoCall.setData(removeproductvideoBean);
			removeproductvideoCall.callService();			
			Logger.verifyEquals("200", removeproductvideoCall.getString("Code"), "验证code");
			ProductVideoExplain pv = liveRoomWapper.selectProductvideoExplain(pid);
			Logger.verifyEquals(pv.getUserId(),sellerId , "验证UserId");
			Logger.verifyEquals(pv.getTaskId()+"",taskId , "验证TaskId");
			Logger.verifyEquals(pv.getLiveId(),createlive.getId() , "验证LiveId");
			Logger.verifyEquals(pv.getExplainStatus(),3, "验证ExplainStatus");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RemoveProductVideo_004() {
		Logger.start(true,"取消录制成功  、计数器+1");
		try {
			
			//创建直播，
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 1秒钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			Date starttime = new Date(now.getTime() + 5000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(livedto.getId(), sellerId, productId);
			Thread.sleep(5000);
			LiveRoomCallService.SellerLiveOnline(livedto.getId(), sellerId);
			//开始直播录制
			String taskId = LiveRoomCallService.CreateProductVideo(livedto.getId(), sellerId, productId);
			//增加mock，消除存量数据
			LinkedHashMap<String,Object> m=new LinkedHashMap<>();
			m.put("mock", "mock-enable");
			m.put("mockId-1", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-1", "liveseller-stopLiveRecord-Normal");
			PayGateWayCallService.addMock(removeproductvideoCall, m);
			
			//结束录制
			removeproductvideoBean.setProductId(productId);
			removeproductvideoBean.setLiveId(livedto.getId().intValue());
			
			removeproductvideoCall.setData(removeproductvideoBean);

			Integer counter = CounterCallService.getSingleValueByLiveId(livedto.getId());
			removeproductvideoCall.callService();	
			Integer counter1 = CounterCallService.getSingleValueByLiveId(livedto.getId());
			Logger.verifyEquals(counter+1, counter1, "添加商品之后，计数器+1");
						
			Logger.verifyEquals("200", removeproductvideoCall.getString("Code"), "验证code");
			ProductVideoExplain pv = liveRoomWapper.selectProductvideoExplain(productId);
			Logger.verifyEquals(pv.getUserId(),sellerId , "验证UserId");
			Logger.verifyEquals(pv.getTaskId()+"",taskId , "验证TaskId");
			Logger.verifyEquals(pv.getLiveId(),livedto.getId() , "验证LiveId");
			Logger.verifyEquals(pv.getExplainStatus(),3 , "验证ExplainStatus");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
}